package com.fzt.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fzt.reggie.common.CustomException;
import com.fzt.reggie.common.R;
import com.fzt.reggie.dto.SetmealDto;
import com.fzt.reggie.entity.Category;
import com.fzt.reggie.entity.Setmeal;
import com.fzt.reggie.entity.SetmealDish;
import com.fzt.reggie.service.CategoryService;
import com.fzt.reggie.service.SetMealService;
import com.fzt.reggie.service.SetmealDishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/*
套餐管理
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetMealService setMealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    /*
    构造分页查询功能
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String name){
        //分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);
        //查询条件
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(name != null,Setmeal::getName,name);

        //排序
        qw.orderByDesc(Setmeal::getUpdateTime);

        //查询结果
        setMealService.page(pageInfo,qw);

        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            //获得分类id
            Long categoryId = item.getCategoryId();
            //根据分类id获得分类名称
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());

        setmealDtoPage.setRecords(list);

        return R.success(setmealDtoPage);
    }

    /*
   新增套餐
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){
        setMealService.saveWithDish(setmealDto);
        return R.success("新增成功");
    }


    /*
    删除套餐
     */
    @DeleteMapping
    @Transactional
    public R<String> delete(@RequestParam List<Long> ids){

        //接受套餐的id
        if (ids.size() != 0) {
            //先将setmeal_dish中的关联id的记录删除
            for (Long id : ids) {
                //获取套餐状态
                Setmeal setmeal = setMealService.getById(id);
                //状态为1则不允许删除
                if (setmeal.getStatus() == 0) {
                    //执行删除操作
                    LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(SetmealDish::getSetmealId,id);
                    setmealDishService.remove(queryWrapper);
                    //删除setmeal中id的相关记录
                    setMealService.removeByIds(ids);


                } else {
                    //不允许删除
                    throw new CustomException(setmeal.getName()+"处于销售状态");
                }


            } return R.success("删除成功");

        } else {
            return R.error("未选择套餐");
        }
    }


    @GetMapping("/list")
    public R<List<Setmeal>> list(@RequestBody Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(setmeal.getCategoryId() !=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(setmeal.getStatus() !=null,Setmeal::getStatus,setmeal.getStatus());
        qw.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setMealService.list(qw);

        return R.success(list);
    }




}



